<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- data：要渲染的数据，必须是一个数组 -->
       <el-table
        stripe
        border
        :data="cateList"
        style="width: 100%">
        <!-- prop：指定要渲染的数据属性名 -->
        <el-table-column
        type="index"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="分类名称"
        >
      </el-table-column>
      <el-table-column
        prop="cate_alias"
        label="分类别名">
      </el-table-column>
         <el-table-column
        label="操作">
        <template v-slot="{ row }">
          <el-button size="mini" type="primary" @click="showEditDialog(row)">修改</el-button>
          <el-button size="mini" type="danger" @click="hDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-card>

      <!-- 添加文章分类弹框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
      :close-on-click-modal="false"
      @closed="$refs.addForm.resetFields()"
      >
      <!--
        上面的 close-on-click-modal 属性表示是否点击遮罩层(modal)关闭弹框，默认值是true
        添加属性的时候要在属性前面加:号，否则等于号后面的值就相当于字符串
        -->
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="hAdd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改文章分类弹框 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="editVisible"
      :close-on-click-modal="false"
      @closed="$refs.editForm.resetFields()"
      width="35%"
      >
      <!--
        上面的 close-on-click-modal 属性表示是否点击遮罩层(modal)关闭弹框，默认值是true
        添加属性的时候要在属性的最前面加 : 号，否则 = 等于号后面的值就相当于字符串
        -->
      <!-- 修改的表单 -->
      <el-form :model="editForm" :rules="addRules" ref="editForm" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="hEdit">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      editVisible: false,
      addVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是 1 ~ 10 位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是 1 ~ 15 位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  // 生命周期钩子函数，页面刚加载的时候执行
  created () {
    this.loadCateList()
  },
  methods: {
    // 获取文章分类列表数据
    async loadCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      // console.log(res)
      this.cateList = res.data
    },
    showEditDialog (row) {
      // 如果id等于1或者2就直接退出，并且提醒用户，管理员不让修改该分类
      if (row.id === 1 || row.id === 2) return this.$message.warning('管理员不允许修改该分类')
      // 做一次深拷贝
      this.editForm = { ...row }
      this.editVisible = true
    },
    hAdd () {
      // 1.兜底校验
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        // 2.校验通过后发送请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        // 3.根据结果提醒用户是否添加成功
        if (res.code !== 0) return
        this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.关闭dialog
        this.addVisible = false
        // 5.重新获取分类列表
        this.loadCateList()
      })
    },
    hEdit () {
      // 1.兜底校验
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        // 2.校验通过后发送请求
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // 3.根据结果提醒用户是否添加成功
        if (res.code !== 0) return
        this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.关闭dialog
        this.editVisible = false
        // 5.重新获取分类列表
        this.loadCateList()
      })
    },
    hDel (id) {
      if (id === 1 || id === 2) return this.$message.waning('不允许删除该分类!')
      this.$confirm('真的要删除吗？', '提示', {
        type: 'waning'
      }).then(async () => {
        // console.log('用户点击了确定')
        // 发请求 delete get 这两个 api 第二个参数都是 config 配置对象
        // put post patch 这三个 api 第二个参数都是 data
        // const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        const { data: res } = await this.$http({
          url: '/my/cate/del',
          method: 'delete',
          params: {
            id
          }
        })

        // 根据结果提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)

        // 更新列表数据
        this.loadCateList()
      }).catch(() => {})// 捕获了取消，但什么也不做
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
